package com.qxsx.midi.activity.exam;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.qxsx.midi.R;
import com.qxsx.midi.activity.base.BaseActivity;
import com.qxsx.midi.config.AppContext;
import com.qxsx.midi.utils.CustomProgress;
import com.qxsx.midi.utils.MyNet;
import com.qxsx.midi.utils.Tools;
import com.qxsx.midi.utils.callBackJsonArray;
import com.qxsx.midi.utils.callBackNull;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by Administrator on 2015/10/16.
 */
public class ExamWaitPayActivity extends BaseActivity implements View.OnClickListener {
    private CustomProgress customProgress;
    private Dialog dialog;  //支付的POP
    private TextView tv_subject, tv_grade, tv_address, tv_time, tv_phone, tv_content;
    private Button btn_cost, btn_tuijian;
    private ImageView iv_img;
    private String id;   //ExamID
    private String major_name, grade_num, exam_site_name, exam_time, tel, detail, cost;
    private Button btn_zhifu;  //支付
    private String order_code; //订单号 ,订单名，订单价格
    private String flag;   //是否支付

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wait_pay_money);
        setTitle("等待缴费");
        setNavBtn(R.mipmap.back, 0);
        Intent intent = getIntent();
        id = intent.getStringExtra("id");//得到ID
        findById();
        examwait();
    }


    private void examwait() {
        customProgress = CustomProgress.show(ExamWaitPayActivity.this, "正在加载中....", true, null);

        JSONObject object = new JSONObject();
        JSONArray array = new JSONArray();

        try {
            object.put("exam_id", id);
            array.put(object);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        MyNet.doPostJsonArray("8012", array, new callBackJsonArray() {
            @Override
            public void run(Object code, Object message, JSONArray array) {
                if (array == null) {
                    return;
                }

                try {
                    JSONObject object1 = array.getJSONObject(0);

                    object1.getString("id");
                    major_name = object1.getString("major_name");
                    grade_num = object1.getString("grade_num");
                    exam_site_name = object1.getString("exam_site_name");
                    exam_time = object1.getString("exam_time");
                    object1.getString("state_code");
                    object1.getString("state_msg");
                    cost = object1.getString("cost");
                    tel = object1.getString("tel");
                    object1.getString("grade_score");
                    //detail = object1.getString("detail");
                    if (major_name.toString().equals("电吉他")) {
                        iv_img.setImageResource(R.mipmap.wait_j);
                    } else if (major_name.toString().equals("电贝司")) {
                        iv_img.setImageResource(R.mipmap.wait_b);
                    } else if (major_name.toString().equals("爵士鼓")) {
                        iv_img.setImageResource(R.mipmap.wait_g);
                    } else if (major_name.toString().equals("音乐基础")) {
                        iv_img.setImageResource(R.mipmap.wait_y);
                    }
                    tv_subject.setText(major_name);
                    tv_grade.setText(grade_num + "级");
                    tv_address.setText(exam_site_name);
                    tv_time.setText(exam_time);
                    tv_phone.setText(tel);
                    //tv_content.setText(detail);
                    btn_cost.setText("¥"+cost);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                customProgress.dismiss();

            }
        });
    }


    /**
     * 是否过时
     */

    private void outmoded(){
        customProgress = CustomProgress.show(ExamWaitPayActivity.this, "正在加载中....", true, null);

        JSONObject object = new JSONObject();
        JSONArray array = new JSONArray();

        try {
            object.put("exam_id", id);
            array.put(object);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        MyNet.doPostJsonArray("518", array, new callBackJsonArray() {
            @Override
            public void run(Object code, Object message, JSONArray array) {
                if (code.toString().equals("200")){
                    getOrderId();
                }else {
                    ShowToast(message.toString());
                }
            }

        });
        customProgress.dismiss();

    }





    private void findById() {
        iv_img = (ImageView) findViewById(R.id.iv_img);
        tv_subject = (TextView) findViewById(R.id.tv_subject);
        tv_grade = (TextView) findViewById(R.id.tv_grade);
        tv_address = (TextView) findViewById(R.id.tv_address);
        tv_time = (TextView) findViewById(R.id.tv_time);
        tv_phone = (TextView) findViewById(R.id.tv_phone);
        tv_content = (TextView) findViewById(R.id.tv_content);
        btn_cost = (Button) findViewById(R.id.btn_cost);
        btn_tuijian = (Button) findViewById(R.id.btn_tuijian);
        btn_tuijian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(AppContext.getContext(), ExamHandBookActivity.class));

            }
        });

        btn_zhifu = (Button) findViewById(R.id.btn_zhifu);
        btn_zhifu.setOnClickListener(this);
    }

    @Override
    protected void HandleTitleBarEvent(TitleBar component, View v) {
        switch (component) {
            case LEFT:
                finish();
                break;
        }
    }


    @Override
    public void onClick(View v) {


        switch (v.getId()) {
            case R.id.btn_zhifu:
                outmoded();
                break;
        }
    }








    /**
     * 获取订单ＩＤ
     */
    private void getOrderId() {
        JSONObject object = new JSONObject();
        JSONArray array = new JSONArray();
        try {
            object.put("exam_id", id);
            object.put("type", "1");
            array.put(object);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        MyNet.doPostJsonArray("516", array, new callBackJsonArray() {
            @Override
            public void run(Object code, Object message, JSONArray array) {

                if (array == null) {
                    Log.e("code", code.toString());
                    Log.e("message", message.toString());
                } else {
                    try {
                        JSONObject object = array.getJSONObject(0);
                        order_code = object.getString("order_num");
                        Log.e("order_code", order_code);
                        dialogbuy();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

            }
        });

    }

    /**
     * 支付的dialog
     */
    private void dialogbuy() {

        LayoutInflater layout = LayoutInflater.from(AppContext.getContext());
        View view = layout.inflate(R.layout.dialog_buy_item, null);
        dialog = new AlertDialog.Builder(ExamWaitPayActivity.this).create();

        TextView tv_weixin = (TextView) view.findViewById(R.id.tv_weixin);
        TextView tv_zhifu = (TextView) view.findViewById(R.id.tv_zhifu);

        tv_zhifu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Tools.AliPayInfo info = new Tools.AliPayInfo("迷笛考级", major_name, cost);
                info.OrderCode = order_code;
                Tools.WeiXinPayInfo info1 = new Tools.WeiXinPayInfo(order_code, (int) (Float.parseFloat(cost) * 100),tv_subject.getText().toString());
                Tools.Pay(info1, info, ExamWaitPayActivity.this, "支付宝", new callBackNull() {
                            @Override
                            public void run() {
                                flag = "1";
                                wreslust();
                                dialog.dismiss();
                            }
                        }, new callBackNull() {
                            @Override
                            public void run() {
                                flag = "0";
                                wreslust();
                                Toast.makeText(ExamWaitPayActivity.this, "支付失败", Toast.LENGTH_SHORT).show();
                                dialog.dismiss();
                            }
                        },
                        null
                );
            }
        });
        /**
         * 微信支付
         */
        tv_weixin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Tools.AliPayInfo info = new Tools.AliPayInfo("迷笛考级", major_name, cost);
                info.OrderCode = order_code;
                Tools.WeiXinPayInfo info1 = new Tools.WeiXinPayInfo(order_code, (int) (Float.parseFloat(cost) * 100),tv_subject.getText().toString());
                Tools.Pay(info1, info, ExamWaitPayActivity.this, "微信", new callBackNull() {
                            @Override
                            public void run() {
                                flag = "1";
                                wreslust();
                                dialog.dismiss();
                            }
                        }, new callBackNull() {
                            @Override
                            public void run() {
                                flag = "0";
                                wreslust();
                                Toast.makeText(ExamWaitPayActivity.this, "支付失败", Toast.LENGTH_SHORT).show();
                                dialog.dismiss();
                            }
                        },
                        null
                );


            }
        });
        TextView tv_clance = (TextView) view.findViewById(R.id.tv_clance);
        tv_clance.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        int x = dm.widthPixels;// 屏幕的宽
        int y = dm.heightPixels;// 屏幕的宽
        dialog = new Dialog(ExamWaitPayActivity.this, R.style.dialog);
        dialog.getWindow().setLayout((int) (x / 2.2), (int) (y / 1.8));
        dialog.show();
        dialog.setContentView(view);
        dialog.setCancelable(false);


    }

    /**
     * 记录支付结果
     */
    private void wreslust() {

        customProgress = CustomProgress.show(ExamWaitPayActivity.this, "请稍后....", true, null);

        JSONObject object = new JSONObject();
        JSONArray array = new JSONArray();

        try {
            object.put("exam_id", id);
            object.put("flag", flag);
            array.put(object);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        MyNet.doPostJsonArray("506", array, new callBackJsonArray() {
            @Override
            public void run(Object code, Object message, JSONArray array) {

                Log.e("code", code.toString());
                Log.e("message", message.toString());
                finish();
                customProgress.dismiss();

            }
        });


    }
}
